package com.xthink.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xthink.common.result.AjaxResult;
import com.xthink.common.pojo.sys.Role;
import com.xthink.common.pojo.sys.RoleMenu;
import com.xthink.common.vo.sys.RoleInsertVo;
import com.xthink.common.vo.sys.RoleParentVo;
import com.xthink.common.vo.sys.RoleUpdateVo;
import com.xthink.web.mapper.RoleMapper;
import com.xthink.web.mapper.RoleMenuMapper;
import com.xthink.web.service.RoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    //添加角色
    @Override
    public AjaxResult insertRole(RoleInsertVo roleInsertVo) {
        Role role = new Role();
        role.setParentId(roleInsertVo.getParentId());
        role.setName(roleInsertVo.getName());
        role.setStatus(roleInsertVo.getStatus());

        int insert = roleMapper.insert(role);
        if (insert==0){
            return AjaxResult.error("新建角色失败");
        }

        Integer id = role.getId();

        Integer[] ids = roleInsertVo.getIds();
        for (int i = 0; i < ids.length; i++) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(id);
            roleMenu.setMenuId(ids[i]);
            int insert1 = roleMenuMapper.insert(roleMenu);
            if (insert1==0){
                return AjaxResult.error("菜单ID:"+ids[i]+"权限，未添加进角色");
            }
        }
        return AjaxResult.success();
    }

    //编辑角色
    @Override
    public AjaxResult updateRole(RoleUpdateVo roleUpdateVo) {
        UpdateWrapper<Role> roleWrapper = new UpdateWrapper<>();
        roleWrapper.eq("id",roleUpdateVo.getIds())
                .set(roleUpdateVo.getParentId()!=null,"parent_id",roleUpdateVo.getParentId())
                .set(!StringUtils.isNotBlank(roleUpdateVo.getName()),"name",roleUpdateVo.getName());
        int update = roleMapper.update(null, roleWrapper);

        if (update==0){
            return AjaxResult.error("修改角色信息失败");
        }

        LambdaQueryWrapper<RoleMenu> roleMenuWrapper = new LambdaQueryWrapper<>();
        roleMenuWrapper.eq(RoleMenu::getRoleId,roleUpdateVo.getId());
        roleMenuMapper.delete(roleMenuWrapper);

        Integer[] ids = roleUpdateVo.getIds();
        for (int i = 0; i < ids.length; i++) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleUpdateVo.getId());
            roleMenu.setMenuId(ids[i]);
            int insert1 = roleMenuMapper.insert(roleMenu);
            if (insert1==0){
                return AjaxResult.error("菜单ID:"+ids[i]+"权限，未添加进角色");
            }
        }
        return AjaxResult.success();
    }

    //查询角色组
    @Override
    public AjaxResult listRole() {
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Role::getDelFlag,0);
        List<Role> roles = roleMapper.selectList(wrapper);
        return AjaxResult.success(roles);
    }

    //删除角色组
    @Override
    public AjaxResult deleteRole(Integer id) {
        UpdateWrapper<Role> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id).set("del_flag",1);
        int update = roleMapper.update(null, wrapper);
        if (update==0){
            return AjaxResult.error("删除角色失败");
        }
        return AjaxResult.success();
    }

    //父级下拉框
    @Override
    public AjaxResult parent() {
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Role::getDelFlag,0);
        List<Role> roles = roleMapper.selectList(wrapper);
        List<RoleParentVo> roleParentVos = new ArrayList<>();
        for (int i = 0; i < roles.size(); i++) {
            RoleParentVo roleParentVo = new RoleParentVo();
            roleParentVo.setId(roles.get(i).getId());
            roleParentVo.setName(roles.get(i).getName());
            roleParentVos.add(roleParentVo);
        }
        return AjaxResult.success(roleParentVos);
    }
}
